
var his={};

(function($,window,document){
	var hiddenfield = '_00hidmgfield';
	/**
	 * [targerColumnArray 对象集合]
	 * [{index:1,field:'orderNum',fieldDesc:'2016001',rowspan:2}]
	 * i -> row index : 行索引. (row index start with 0)
	 * ft -> fieldText : 字段在对应行对应列的值.
	 * rs -> rowspan : 有几行需要合并.
	 * @type {Array}
	 */
	var targerColumnArray = [];
	/**
	 * [targetField 字段名称. 列名.]
	 * @type {String}
	 */
	var targetField = '';
	var temparr = [];
	/**
	 * [tempobj description]
	 * {k1:[a,b],k2:[a,b],k3:[a,b]...}
	 * @type {Object}
	 */
	var tempobj = {};
	function setarr(tca){
		targerColumnArray = tca;
	}
	function getarr(){
		return targerColumnArray;
	}
	function add(i, ft, rs){
		var obj = {};
		obj['i']  = i;
		obj['ft'] = ft;
		obj['rs'] = rs;
		targerColumnArray.push(obj);
	}
	/**
	 * [initTargetColumnArray description]
	 * @param  {[type]} data  [easyui datagrid 的数据(data)]
	 * @param  {[type]} field [需要合并的字段对象]
	 * @return {[type]}       [description]
	 */
	function initTargetColumnArray(data,field){
		var f = targetField = field;
		setarr([]);
		var rows = null;
		if(!!data && data['total']>0){
			rows = data['rows'];
		}else{
			return;
		}
		var ft = '', count = 0;
		$.each(rows, function(i, row){
			var obj = {};
			//修改rows对象，添加一个字段, 作为程序的扩展。hidden merge field.
			rows[i][hiddenfield] = rows[i][f];
			count = count + 1;
			if(i==0){
				ft = row[f];
				temparr.push( ft );
				tempobj[ft] = [count, i];
			}else{
				if(ft != row[f]){
					ft = row[f];
					count = 1; // count初始化为'1'
					temparr.push( ft );
					tempobj[ft] = [count, i];
				}else{
					tempobj[ft][0] = count;
				}
			}
		});
		$.each(temparr, function(i,n){
			add(tempobj[n][1], n, tempobj[n][0]);
		});
		temparr = [], tempobj = {};
	}
	function updateRow(me){
		$.each(targerColumnArray, function(i,n){
			if(n['rs'] > 1){
				$(me).datagrid('updateRow',{
					index: n['i'],
					row: { targetField: n['ft'] }
				});
				$(me).datagrid('mergeCells',{
					index: n['i'],
					field: targetField,
					rowspan: n['rs']
				});
			}
		});
	}
	function selectRows(me,index,row){
		$(me).datagrid('updateRow', {
			index: index,
			row: {_is_sel:'Y'}
		});
		updateRow(me);
	}
	function unSelectRows(me,index,row){
		$(me).datagrid('updateRow', {
			index: index,
			row: {_is_sel:'N'}
		});
		updateRow(me);
	}
	function clickRows(me,index,row){
		var rows = $(me).datagrid('getRows');
		$.each(rows, function(i,n){
			if(n[hiddenfield] == row[hiddenfield]){
				if('N' == row['_is_sel']){
					$(me).datagrid('unselectRow', i);
				}else{
					$(me).datagrid('selectRow', i);
				}
			}
		});
	}
	window.mergeDatagridRow = {
		init: initTargetColumnArray,
		getarr: getarr,
		updateRow: updateRow,
		selectRows: selectRows,
		unSelectRows: unSelectRows,
		clickRows: clickRows
	};
})(jQuery,window,document);

var mdr = mergeDatagridRow;

var sub_grid_data = [
	{orderNum:2016001,orderdate:'08/23/2012',shippeddate:'12/25/2012',freight:9734},
	{orderNum:2016001,orderdate:'08/24/2012',shippeddate:'12/26/2012',freight:9735},
	{orderNum:2016002,orderdate:'08/25/2012',shippeddate:'12/27/2012',freight:9736},
	{orderNum:2016003,orderdate:'08/26/2012',shippeddate:'12/28/2012',freight:9737}
];

function func_loadsuccess(data, That){
	mdr.init(data, 'orderNum');
	mdr.updateRow( That );
}

function func_select(That,index,row){
	mdr.selectRows(That,index,row);
}

function func_unselect(That,index,row){
	mdr.unSelectRows(That,index,row);
}

function func_clickrow(That,index,row){
	mdr.clickRows(That,index,row);
}

var conf = {
	options:{
		fitColumns:true,
		rownumbers:true,
		columns:[[
			{field:'company',title:'Company Name',width:200},
			{field:'contact',title:'Contact Name',width:200},
			{field:'country',title:'Country',width:200}
		]],
		data:[
			{company:'Speed Info',contact:'Minna John',country:'Sweden'}
		]
	},
	subgrid:{
		options:{
			fitColumns:true,
			rownumbers:true,
			foreignField:'companyid',
			columns:[[
				{field:'orderNum',title:'Order Number',width:200},
				{field:'orderdate',title:'Order Date',width:200},
				{field:'shippeddate',title:'Shipped Date',width:200},
				{field:'freight',title:'Freight',width:200}
			]],
			data: sub_grid_data,
			onLoadSuccess: function(data){
				var That = this;
				func_loadsuccess(data,That);
			},
			onSelect: function(index,row){
				var That = this;
				func_select(That,index,row);
			},
			onUnselect: function(index,row){
				var That = this;
				func_unselect(That,index,row);
			},
			onClickRow: function(index,row){
				var That = this;
				func_clickrow(That,index,row);
			}
		}
	}
};

$(function(){
	$('#tt').datagrid({
		title:'DataGrid - Nested SubGrid',
		width:700,
		height:300
	}).datagrid('subgrid', conf);
});